package usePriorityQueue;

import java.util.Comparator;
import java.util.Date;
import java.util.PriorityQueue;

/*
 * java中优先队列的使用
 *
 * */
public class demo {
    public static void main(String[] args) {
        // 从小到大排列
        // 创建优先队列
        PriorityQueue<Integer> queue = new PriorityQueue<>();
        // 添加元素
        queue.add(1);
        // 删除队列中的所有元素
        queue.clear();
        //队列中是否包含此元素
        boolean contains = queue.contains(1);
        System.out.println(contains);
        //将指定元素插入此队列中 排序插入
        queue.offer(1);
        queue.offer(2);
        queue.offer(3);
        queue.offer(4);
        // 返回队列中的元素个数
        int size = queue.size();
        System.out.println("size = " + size);//4
        //检索队列头但不删除元素
        Integer peek = queue.peek();
        System.out.println(peek);
        // 检索并删除此队列的头
        Integer poll = queue.poll();
        System.out.println(poll);

        // 传入一个比较器根据指定的比较内容进行比较
        PriorityQueue<Dog> queue1 = new PriorityQueue<Dog>(new Comparator<Dog>() {
            @Override
            public int compare(Dog o1, Dog o2) {
                return o1.age - o2.age;
            }
        });
        Dog tom = new Dog("tom", 1);
        Dog tom1 = new Dog("tom", 2);
        Dog tom2 = new Dog("tom", 3);
        Dog tom3 = new Dog("tom", 4);
        queue1.offer(tom);
        queue1.offer(tom1);
        queue1.offer(tom2);
        queue1.offer(tom3);
        Dog peek1 = queue1.peek();
        Dog poll1 = queue1.poll();
        System.out.println("poll1 = " + poll1);
        peek1 = queue1.peek();
        System.out.println(peek1);
    }
}

